<template>
  <!-- components: Modal -->
  <teleport to="#modal">
    <div class = "modal d-block" tabindex = "-1" v-if="visible">
      <div class = "modal-dialog">
        <div class = "modal-content">
          <div class = "modal-header">
            <h5 class = "modal-title">{{title}}</h5>
            <button
              class = "btn-close"
              type = "button"
              data-dismiss = "modal"
              aria-label = "Close"
            >
              <span aria-hidden="true" v-on:click="onClose">&times;</span>
            </button>
          </div>
          <div class = "modal-body">
            <slot></slot>
          </div>
          <div class = "modal-footer">
            <button
              class = "btn btn-secondary"
              type = "button"
              data-dismiss = "modal"
              v-on:click="onClose"
            >取消</button>
            <button
              class = "btn btn-primary"
              type = "button"
              v-on:click="onConfirm"
            >确定</button>
          </div>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import useDOMCreate from '@/hooks/useDOMCreate'

// 对setup函数进行封装，返回options的对象
export default defineComponent({
  // 页面名称
  name: 'Modal',
  // 传递数据
  props: {
    title: String,
    visible: {
      type: Boolean,
      default: false
    }
  },
  // 发出事件
  emits: ['modal-on-close', 'modal-on-confirm'],

  // 组合式api入口
  setup (props, context) {
    useDOMCreate('modal')
    const onClose = () => {
      context.emit('modal-on-close')
    }
    const onConfirm = () => {
      context.emit('modal-on-confirm')
    }
    return {
      onClose,
      onConfirm
    }
  }
})
</script>

<style lang = "css" scoped>
</style>
